Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ---------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts ------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

p_combined <-
    plotly::subplot(p2,p1, # plots to combine, top to bottom
      nrows = 2,
      heights = c(.6,.4),  # relative heights of the two plots
      shareX = TRUE,  # plots will share an X axis
      titleY = TRUE
    ) %>%
    # create a vertical "spike line" to compare data across 2 plots
    plotly::layout(
      xaxis = list(
        spikethickness = 1,
        spikedash = "dot",
        spikecolor = "black",
        spikemode = "across+marker",
        spikesnap = "cursor"
      ),
      yaxis = list(spikethickness = 0)
    )
## Warning: Ignoring 1 observations
p_combined

Save the plot to pull into the index

#save(p_combined, file = "./plotly_fig.rda")

Save an htmlwidget for website embedding

#htmlwidgets::saveWidget(p_combined, "plotly_fig.html")
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#build a function here to make smooth frames so we don't repeat everything in huge loops
#FOR INDIVIDUAL FIGURES ONLY
make_n1_smooth_frame <- function(df){
  smooth_n1 <- df %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N1")
  return(smooth_n1)
}

make_n2_smooth_frame <- function(df){
  smooth_n1 <- df %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N2")
  return(smooth_n1)
}

#run frames through the functions
wrfa_smooth_n1 <- make_n1_smooth_frame(wrf_a_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfb_smooth_n1 <- make_n1_smooth_frame(wrf_b_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfc_smooth_n1 <- make_n1_smooth_frame(wrf_c_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfa_smooth_n2 <- make_n2_smooth_frame(wrf_a_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfb_smooth_n2 <- make_n2_smooth_frame(wrf_b_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfc_smooth_n2 <- make_n2_smooth_frame(wrf_c_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#get max date
maxdate <- max(wrfa_smooth_n1$date)
mindate <- min(wrfa_smooth_n1$date)

Build loess smoothing figures figures

#COMBINED FIGURE ONLY
#create smoothing data frames 
#n1
smooth_n1 <- only_n1 %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N1")
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#n2
smooth_n2 <- only_n2 %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N2")
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#**************************************COMBINED PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1 <- ggplot(smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 154)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2 <- ggplot(smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 154)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1
## `geom_smooth()` using formula 'y ~ x'

fit_n1
##   [1] 11.58770 11.66753 11.74674 11.82456 11.90023 11.97298 12.04206 12.10670
##   [9] 12.16756 12.22589 12.28181 12.33543 12.38687 12.43625 12.48368 12.52927
##  [17] 12.57315 12.61543 12.65623 12.69566 12.73384 12.77088 12.80515 12.83514
##  [25] 12.86119 12.88359 12.90266 12.91873 12.93210 12.94310 12.95204 12.95923
##  [33] 12.96499 12.96964 12.97349 12.97686 12.98006 12.98341 12.98723 12.99183
##  [41] 12.99753 13.00464 13.01348 13.02219 13.02888 13.03385 13.03740 13.03981
##  [49] 13.04137 13.04238 13.04313 13.04390 13.04499 13.04670 13.04931 13.05312
##  [57] 13.05841 13.06548 13.07462 13.08612 13.10028 13.11738 13.13771 13.16158
##  [65] 13.19128 13.22835 13.27189 13.32100 13.37479 13.43236 13.49283 13.55528
##  [73] 13.61884 13.68260 13.74567 13.80716 13.86616 13.92179 13.97314 14.01933
##  [81] 14.05946 14.09264 14.11796 14.13454 14.14147 14.13820 14.12532 14.10370
##  [89] 14.07421 14.03771 13.99506 13.94715 13.89482 13.83896 13.78041 13.72006
##  [97] 13.65876 13.59739 13.53680 13.47787 13.42146 13.36843 13.31966 13.27601
## [105] 13.23834 13.20753 13.18153 13.15768 13.13589 13.11608 13.09816 13.08206
## [113] 13.06768 13.05494 13.04376 13.03406 13.02574 13.01873 13.01294 13.00829
## [121] 13.00469 13.00206 13.00031 12.99936 12.99913 12.99953 13.00048 13.00261
## [129] 13.00654 13.01216 13.01938 13.02807 13.03814 13.04948 13.06198 13.07554
## [137] 13.09005 13.10540 13.12148 13.13820 13.15544 13.17310 13.19125 13.21006
## [145] 13.22955 13.24975 13.27068 13.29237 13.31485 13.33814 13.36226 13.38725
## [153] 13.41312 13.43991
#n2
extract_n2
## `geom_smooth()` using formula 'y ~ x'

fit_n2
##   [1] 11.37120 11.49056 11.60835 11.72377 11.83601 11.94427 12.04774 12.14563
##   [9] 12.23859 12.32794 12.41379 12.49627 12.57551 12.65164 12.72478 12.79506
##  [17] 12.86260 12.92754 12.98999 13.05009 13.10797 13.16374 13.21575 13.26246
##  [25] 13.30419 13.34126 13.37400 13.40272 13.42774 13.44939 13.46799 13.48385
##  [33] 13.49731 13.50867 13.51825 13.52639 13.53340 13.53960 13.54532 13.55087
##  [41] 13.55657 13.56274 13.56972 13.57448 13.57418 13.56936 13.56058 13.54840
##  [49] 13.53336 13.51601 13.49691 13.47661 13.45566 13.43461 13.41402 13.39443
##  [57] 13.37640 13.36047 13.34721 13.33716 13.33088 13.32891 13.33181 13.34013
##  [65] 13.35450 13.37472 13.40015 13.43012 13.46399 13.50110 13.54078 13.58240
##  [73] 13.62528 13.66878 13.71225 13.75501 13.79643 13.83583 13.87258 13.90601
##  [81] 13.93546 13.96029 13.97983 13.99343 14.00044 14.00150 13.99794 13.99011
##  [89] 13.97836 13.96304 13.94450 13.92310 13.89918 13.87309 13.84520 13.81585
##  [97] 13.78538 13.75416 13.72253 13.69084 13.65946 13.62871 13.59897 13.57058
## [105] 13.54389 13.51925 13.49603 13.47331 13.45100 13.42902 13.40729 13.38571
## [113] 13.36422 13.34271 13.32111 13.29934 13.27731 13.25493 13.23213 13.20881
## [121] 13.18490 13.16030 13.13495 13.10874 13.08160 13.05344 13.02419 12.99443
## [129] 12.96476 12.93505 12.90517 12.87500 12.84441 12.81329 12.78150 12.74893
## [137] 12.71545 12.68093 12.64526 12.60830 12.56994 12.53005 12.48883 12.44659
## [145] 12.40336 12.35917 12.31404 12.26801 12.22111 12.17337 12.12481 12.07548
## [153] 12.02539 11.97458
#assign fits to a vector
n1_trend <- fit_n1
n2_trend <- fit_n2

#extract y min and max for each
limits_n1 <- ggplot_build(extract_n1)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1 <- as.data.frame(limits_n1)
n1_ymin <- limits_n1$ymin
n1_ymax <- limits_n1$ymax

limits_n2 <- ggplot_build(extract_n2)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2 <- as.data.frame(limits_n2)
n2_ymin <- limits_n2$ymin
n2_ymax <- limits_n2$ymax

#reassign dataframes (just to be safe)
work_n1 <- smooth_n1
work_n2 <- smooth_n2

#fill in missing dates to smooth fits
work_n1 <- work_n1 %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1 <- work_n1$date
work_n2 <- work_n2 %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2 <- work_n2$date

#create a new smooth dataframe to layer
smooth_frame_n1 <- data.frame(date_vec_n1, n1_trend, n1_ymin, n1_ymax)
smooth_frame_n2 <- data.frame(date_vec_n2, n2_trend, n2_ymin, n2_ymax)
#make plotlys
#**************************************COMBINED PLOT**********************************************
#plot smooth frames
p3 <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1, y = ~n1_trend,
                    data = smooth_frame_n1,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1,
                                  '</br> Median Log Copies: ', round(n1_trend, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_lines(x = ~date_vec_n2, y = ~n2_trend,
                  data = smooth_frame_n2,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2,
                                  '</br> Median Log Copies: ', round(n2_trend, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1, ymin = ~n1_ymin, ymax = ~n1_ymax,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymax, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_ymin, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2, ymin = ~n2_ymin, ymax = ~n2_ymax,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymax, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_ymin, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p3

Create final trend plot by stacking with epidemic curve

smooth_extracttest <-
    plotly::subplot(p3,p1, # plots to combine, top to bottom
      nrows = 2,
      heights = c(.6,.4),  # relative heights of the two plots
      shareX = TRUE,  # plots will share an X axis
      titleY = TRUE
    ) %>%
    # create a vertical "spike line" to compare data across 2 plots
    plotly::layout(
      xaxis = list(
        spikethickness = 1,
        spikedash = "dot",
        spikecolor = "black",
        spikemode = "across+marker",
        spikesnap = "cursor"
      ),
      yaxis = list(spikethickness = 0)
    )
## Warning: Ignoring 1 observations
smooth_extracttest
#save(smooth_extracttest, file = "./smooth_extracttest.rda")

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1a <- ggplot(wrfa_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1a<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 154)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2a <- ggplot(wrfa_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2a<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 154)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1a
## `geom_smooth()` using formula 'y ~ x'

fit_n1a
##   [1] 11.40895 11.43812 11.46755 11.49674 11.52517 11.55231 11.57764 11.60065
##   [9] 11.62183 11.64213 11.66166 11.68057 11.69896 11.71698 11.73476 11.75159
##  [17] 11.76687 11.78079 11.79358 11.80544 11.81659 11.82724 11.83760 11.84790
##  [25] 11.85833 11.86912 11.88047 11.89260 11.90573 11.91557 11.91934 11.91946
##  [33] 11.91836 11.91846 11.92219 11.93198 11.94190 11.94526 11.94334 11.93741
##  [41] 11.92876 11.91865 11.90837 11.89919 11.89240 11.88926 11.89106 11.89907
##  [49] 11.91458 11.93885 11.97642 12.02778 12.08817 12.15281 12.21693 12.27577
##  [57] 12.32455 12.37557 12.44244 12.52239 12.61263 12.71037 12.81283 12.91724
##  [65] 13.02079 13.12072 13.21423 13.29855 13.37088 13.42845 13.46847 13.50863
##  [73] 13.56365 13.62628 13.68928 13.74540 13.78741 13.80806 13.80879 13.79697
##  [81] 13.77429 13.74244 13.70309 13.65794 13.60866 13.55694 13.50446 13.45291
##  [89] 13.40397 13.35931 13.32064 13.28962 13.24471 13.17077 13.07911 12.98104
##  [97] 12.88788 12.81094 12.76155 12.72388 12.67589 12.61954 12.55678 12.48956
## [105] 12.41983 12.34955 12.28066 12.21513 12.15491 12.10194 12.05818 12.02558
## [113] 12.00610 12.00876 12.03523 12.07640 12.12318 12.16646 12.19715 12.20615
## [121] 12.20363 12.20545 12.21105 12.21988 12.23137 12.24495 12.26007 12.27617
## [129] 12.29268 12.30904 12.32469 12.33907 12.35162 12.36178 12.37235 12.38592
## [137] 12.40161 12.41854 12.43583 12.45260 12.46797 12.48107 12.49300 12.50533
## [145] 12.51789 12.53047 12.54290 12.55497 12.56650 12.57585 12.58236 12.58733
## [153] 12.59208 12.59791
#n2
extract_n2a
## `geom_smooth()` using formula 'y ~ x'

fit_n2a
##   [1] 11.00926 11.15038 11.28893 11.42414 11.55527 11.68157 11.80229 11.91667
##   [9] 12.02544 12.12994 12.23034 12.32680 12.41948 12.50856 12.59419 12.67535
##  [17] 12.75111 12.82180 12.88775 12.94931 13.00680 13.06055 13.11091 13.15820
##  [25] 13.20276 13.24491 13.28500 13.32335 13.36031 13.39091 13.41177 13.42561
##  [33] 13.43511 13.44299 13.45193 13.46464 13.47132 13.46200 13.43910 13.40506
##  [41] 13.36233 13.31334 13.26052 13.20632 13.15316 13.10349 13.05975 13.02436
##  [49] 12.99978 12.98842 12.98229 12.97291 12.96203 12.95140 12.94274 12.93782
##  [57] 12.93837 12.94943 12.97344 13.00824 13.05167 13.10156 13.15576 13.21210
##  [65] 13.26843 13.32258 13.37239 13.41571 13.45036 13.47420 13.48506 13.49848
##  [73] 13.52696 13.56376 13.60214 13.63537 13.65672 13.65943 13.64537 13.62178
##  [81] 13.59004 13.55153 13.50762 13.45970 13.40914 13.35731 13.30559 13.25537
##  [89] 13.20801 13.16490 13.12741 13.09692 13.05697 12.99581 12.92184 12.84350
##  [97] 12.76920 12.70737 12.66643 12.63559 12.59920 12.55835 12.51416 12.46774
## [105] 12.42019 12.37262 12.32614 12.28185 12.24087 12.20430 12.17325 12.14882
## [113] 12.13213 12.13071 12.14683 12.17372 12.20463 12.23279 12.25142 12.25378
## [121] 12.24841 12.24784 12.25128 12.25789 12.26688 12.27742 12.28870 12.29992
## [129] 12.31025 12.31890 12.32503 12.32785 12.32653 12.32027 12.31256 12.30681
## [137] 12.30208 12.29743 12.29192 12.28462 12.27458 12.26087 12.24451 12.22712
## [145] 12.20860 12.18888 12.16788 12.14554 12.12176 12.09491 12.06421 12.03090
## [153] 11.99623 11.96145
#assign fits to a vector
n1_trenda <- fit_n1a
n2_trenda <- fit_n2a

#extract y min and max for each
limits_n1a <- ggplot_build(extract_n1a)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1a <- as.data.frame(limits_n1a)
n1_ymina <- limits_n1a$ymin
n1_ymaxa <- limits_n1a$ymax

limits_n2a <- ggplot_build(extract_n2a)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2a <- as.data.frame(limits_n2a)
n2_ymina <- limits_n2a$ymin
n2_ymaxa <- limits_n2a$ymax

#reassign dataframes (just to be safe)
work_n1a <- wrfa_smooth_n1
work_n2a<- wrfa_smooth_n1

#fill in missing dates to smooth fits
work_n1a <- work_n1a %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1a <- work_n1a$date
work_n2a <- work_n2a %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2a <- work_n2a$date

#create a new smooth dataframe to layer
smooth_frame_n1a <- data.frame(date_vec_n1a, n1_trenda, n1_ymina, n1_ymaxa)
smooth_frame_n2a <- data.frame(date_vec_n2a, n2_trenda, n2_ymina, n2_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1a, y = ~n1_trenda,
                    data = smooth_frame_n1a,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1a,
                                  '</br> Median Log Copies: ', round(n1_trenda, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2a, y = ~n2_trenda,
                  data = smooth_frame_n2a,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2a,
                                  '</br> Median Log Copies: ', round(n2_trenda, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1a, ymin = ~n1_ymina, ymax = ~n1_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1a, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_ymina, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2a, ymin = ~n2_ymina, ymax = ~n2_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2a, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_ymina, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfa_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfa_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1b <- ggplot(wrfb_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1b<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 154)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2b <- ggplot(wrfb_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2b<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 154)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1b
## `geom_smooth()` using formula 'y ~ x'

fit_n1b
##   [1] 11.24146 11.29068 11.33992 11.38839 11.43530 11.47986 11.52126 11.55872
##   [9] 11.59292 11.62517 11.65558 11.68427 11.71137 11.73699 11.76125 11.78427
##  [17] 11.80617 11.82707 11.84709 11.86634 11.88496 11.90305 11.91888 11.93085
##  [25] 11.93930 11.94457 11.94700 11.94693 11.94470 11.94064 11.93510 11.92841
##  [33] 11.92092 11.91296 11.90486 11.89698 11.88964 11.88319 11.87797 11.87431
##  [41] 11.87256 11.87304 11.87611 11.87988 11.88244 11.88405 11.88499 11.88552
##  [49] 11.88594 11.88649 11.88747 11.88913 11.89176 11.89563 11.90100 11.90815
##  [57] 11.91736 11.92890 11.94303 11.96003 11.98018 12.00375 12.03100 12.06222
##  [65] 12.10128 12.15106 12.21035 12.27793 12.35257 12.43305 12.51816 12.60668
##  [73] 12.69738 12.78904 12.88045 12.97038 13.05761 13.14092 13.21910 13.29092
##  [81] 13.35515 13.41059 13.45601 13.49020 13.51192 13.52096 13.51856 13.50572
##  [89] 13.48340 13.45259 13.41426 13.36940 13.31898 13.26399 13.20541 13.14421
##  [97] 13.08138 13.01789 12.95472 12.89286 12.83328 12.77697 12.72489 12.67805
## [105] 12.63740 12.60394 12.57495 12.54708 12.52037 12.49487 12.47059 12.44760
## [113] 12.42591 12.40557 12.38661 12.36907 12.35299 12.33840 12.32534 12.31385
## [121] 12.30397 12.29572 12.28916 12.28431 12.28121 12.27990 12.28041 12.28274
## [129] 12.28685 12.29273 12.30036 12.30973 12.32083 12.33365 12.34818 12.36440
## [137] 12.38230 12.40188 12.42311 12.44599 12.47051 12.49666 12.52431 12.55338
## [145] 12.58387 12.61578 12.64913 12.68391 12.72013 12.75779 12.79691 12.83749
## [153] 12.87953 12.92304
#n2
extract_n2b
## `geom_smooth()` using formula 'y ~ x'

fit_n2b
##   [1] 10.90442 10.98871 11.07222 11.15420 11.23389 11.31053 11.38337 11.45166
##   [9] 11.51601 11.57765 11.63669 11.69324 11.74742 11.79933 11.84910 11.89683
##  [17] 11.94263 11.98663 12.02893 12.06965 12.10890 12.14679 12.18178 12.21246
##  [25] 12.23914 12.26213 12.28172 12.29824 12.31199 12.32327 12.33240 12.33969
##  [33] 12.34544 12.34996 12.35356 12.35654 12.35922 12.36191 12.36491 12.36853
##  [41] 12.37308 12.37886 12.38619 12.39155 12.39163 12.38705 12.37844 12.36642
##  [49] 12.35162 12.33466 12.31617 12.29676 12.27707 12.25772 12.23933 12.22253
##  [57] 12.20794 12.19619 12.18789 12.18368 12.18418 12.19001 12.20180 12.22017
##  [65] 12.24925 12.29173 12.34613 12.41095 12.48469 12.56586 12.65297 12.74452
##  [73] 12.83900 12.93494 13.03083 13.12518 13.21649 13.30326 13.38401 13.45723
##  [81] 13.52144 13.57513 13.61681 13.64499 13.65817 13.65664 13.64244 13.61664
##  [89] 13.58034 13.53462 13.48058 13.41930 13.35187 13.27938 13.20292 13.12358
##  [97] 13.04245 12.96061 12.87916 12.79918 12.72176 12.64799 12.57896 12.51576
## [105] 12.45948 12.41120 12.36827 12.32724 12.28801 12.25050 12.21460 12.18022
## [113] 12.14728 12.11566 12.08529 12.05606 12.02787 12.00065 11.97429 11.94869
## [121] 11.92377 11.89942 11.87556 11.85209 11.82891 11.80593 11.78306 11.76122
## [129] 11.74126 11.72301 11.70630 11.69095 11.67679 11.66363 11.65130 11.63964
## [137] 11.62845 11.61757 11.60681 11.59601 11.58499 11.57357 11.56188 11.55018
## [145] 11.53852 11.52695 11.51551 11.50427 11.49326 11.48253 11.47213 11.46210
## [153] 11.45250 11.44338
#assign fits to a vector
n1_trendb <- fit_n1b
n2_trendb <- fit_n2b

#extract y min and max for each
limits_n1b <- ggplot_build(extract_n1b)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1b <- as.data.frame(limits_n1b)
n1_yminb <- limits_n1b$ymin
n1_ymaxb <- limits_n1b$ymax

limits_n2b <- ggplot_build(extract_n2b)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2b <- as.data.frame(limits_n2b)
n2_yminb <- limits_n2b$ymin
n2_ymaxb <- limits_n2b$ymax

#reassign dataframes (just to be safe)
work_n1b <- wrfb_smooth_n1
work_n2b<- wrfb_smooth_n1

#fill in missing dates to smooth fits
work_n1b <- work_n1b %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1b <- work_n1b$date
work_n2b <- work_n2b %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2b <- work_n2b$date

#create a new smooth dataframe to layer
smooth_frame_n1b <- data.frame(date_vec_n1b, n1_trendb, n1_yminb, n1_ymaxb)
smooth_frame_n2b <- data.frame(date_vec_n2b, n2_trendb, n2_yminb, n2_ymaxb)

#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1b, y = ~n1_trendb,
                    data = smooth_frame_n1b,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1b,
                                  '</br> Median Log Copies: ', round(n1_trendb, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2b, y = ~n2_trendb,
                  data = smooth_frame_n2b,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2b,
                                  '</br> Median Log Copies: ', round(n2_trendb, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1b, ymin = ~n1_yminb, ymax = ~n1_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1b, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_yminb, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2b, ymin = ~n2_yminb, ymax = ~n2_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2b, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_yminb, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfb_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfb_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** Does not work until raw data fixed #add trendlines #extract data from geom_smooth #n1 extract # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_n1c <- ggplot(wrfc_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1c<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 140)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2c <- ggplot(wrfc_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2c<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 140)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1c
## `geom_smooth()` using formula 'y ~ x'

fit_n1c
##   [1] 11.13970 11.20016 11.25926 11.31686 11.37279 11.42690 11.47902 11.52900
##   [9] 11.57697 11.62319 11.66770 11.71052 11.75170 11.79127 11.82926 11.86571
##  [17] 11.90066 11.93413 11.96616 11.99680 12.02606 12.05399 12.07987 12.10308
##  [25] 12.12381 12.14229 12.15870 12.17326 12.18616 12.19762 12.20782 12.21698
##  [33] 12.22530 12.23298 12.24023 12.24724 12.24687 12.23462 12.21471 12.19135
##  [41] 12.16876 12.15114 12.14272 12.14230 12.14548 12.15166 12.16022 12.17058
##  [49] 12.18213 12.19426 12.20636 12.21785 12.22811 12.23654 12.24253 12.24549
##  [57] 12.24481 12.25016 12.26805 12.29308 12.31989 12.34310 12.35732 12.35719
##  [65] 12.34563 12.32946 12.30910 12.28498 12.25751 12.22712 12.19423 12.15926
##  [73] 12.12264 12.08479 12.04613 12.00708 11.96807 11.92952 11.88692 11.83640
##  [81] 11.77932 11.71701 11.65084 11.58215 11.51229 11.44263 11.37450 11.30926
##  [89] 11.24826 11.19285 11.14439 11.10422 11.04416 10.94553 10.82395 10.69506
##  [97] 10.57449 10.47785 10.42078 10.39593 10.38457 10.38520 10.39634 10.41651
## [105] 10.44421 10.47797 10.51631 10.55772 10.60074 10.64388 10.68564 10.72456
## [113] 10.75913 10.79560 10.83987 10.88996 10.94390 10.99972 11.05544 11.10908
## [121] 11.16370 11.22310 11.28650 11.35307 11.42201 11.49250 11.56375 11.63494
## [129] 11.70748 11.78292 11.86064 11.94003 12.02046 12.10135 12.18278 12.26515
## [137] 12.34885 12.43428 12.52183 12.61189
#n2
extract_n2c
## `geom_smooth()` using formula 'y ~ x'

fit_n2c
##   [1] 11.64116 11.65086 11.66083 11.67087 11.68076 11.69031 11.69928 11.70749
##   [9] 11.71512 11.72254 11.72980 11.73694 11.74403 11.75111 11.75822 11.76542
##  [17] 11.77275 11.78026 11.78801 11.79605 11.80441 11.81315 11.82179 11.82988
##  [25] 11.83754 11.84489 11.85205 11.85913 11.86627 11.87357 11.88116 11.88916
##  [33] 11.89768 11.90685 11.91678 11.92759 11.93261 11.92777 11.91707 11.90453
##  [41] 11.89414 11.88991 11.89586 11.91556 11.94842 11.99224 12.04477 12.10379
##  [49] 12.16707 12.23240 12.29753 12.36025 12.41832 12.46952 12.51163 12.54241
##  [57] 12.55964 12.59153 12.65818 12.74517 12.83810 12.92255 12.98411 13.00838
##  [65] 13.00020 12.97553 12.93664 12.88578 12.82521 12.75720 12.68400 12.60788
##  [73] 12.53110 12.45591 12.38458 12.31937 12.26253 12.21634 12.17207 12.12035
##  [81] 12.06247 11.99970 11.93333 11.86464 11.79491 11.72541 11.65743 11.59225
##  [89] 11.53116 11.47542 11.42633 11.38515 11.34936 11.31564 11.28411 11.25491
##  [97] 11.22817 11.20401 11.18256 11.16586 11.15526 11.14985 11.14874 11.15104
## [105] 11.15584 11.16224 11.16936 11.17628 11.18211 11.18596 11.18692 11.18411
## [113] 11.17661 11.16561 11.15319 11.13984 11.12603 11.11225 11.09897 11.08668
## [121] 11.07472 11.06221 11.04930 11.03611 11.02277 11.00944 10.99623 10.98328
## [129] 10.97022 10.95673 10.94308 10.92951 10.91627 10.90350 10.89108 10.87892
## [137] 10.86692 10.85496 10.84294 10.83077
#assign fits to a vector
n1_trendc <- fit_n1c
n2_trendc <- fit_n2c

#extract y min and max for each
limits_n1c <- ggplot_build(extract_n1c)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1c <- as.data.frame(limits_n1c)
n1_yminc <- limits_n1c$ymin
n1_ymaxc <- limits_n1c$ymax

limits_n2c <- ggplot_build(extract_n2c)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2c <- as.data.frame(limits_n2c)
n2_yminc <- limits_n2c$ymin
n2_ymaxc <- limits_n2c$ymax

#reassign dataframes (just to be safe)
work_n1c <- wrfc_smooth_n1
work_n2c <- wrfc_smooth_n1

#fill in missing dates to smooth fits
work_n1c <- work_n1c %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1c <- work_n1c$date
work_n2c <- work_n2c %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2c <- work_n2c$date

#create a new smooth dataframe to layer
smooth_frame_n1c <- data.frame(date_vec_n1c, n1_trendc, n1_yminc, n1_ymaxc)
smooth_frame_n2c <- data.frame(date_vec_n2c, n2_trendc, n2_yminc, n2_ymaxc)


#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1c, y = ~n1_trendc,
                    data = smooth_frame_n1c,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1c,
                                  '</br> Median Log Copies: ', round(n1_trendc, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
   layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2c, y = ~n2_trendc,
                  data = smooth_frame_n2c,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2c,
                                  '</br> Median Log Copies: ', round(n2_trendc, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1c, ymin = ~n1_yminc, ymax = ~n1_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1c, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_yminc, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2c, ymin = ~n2_yminc, ymax = ~n2_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2c, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_yminc, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfc_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfc_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(smooth_frame_n1a, file = "./plotly_objs/smooth_frame_n1a.rda")
save(smooth_frame_n2a, file = "./plotly_objs/smooth_frame_n2a.rda")
save(smooth_frame_n1b, file = "./plotly_objs/smooth_frame_n1b.rda")
save(smooth_frame_n2b, file = "./plotly_objs/smooth_frame_n2b.rda")
save(smooth_frame_n1c, file = "./plotly_objs/smooth_frame_n1c.rda")
save(smooth_frame_n2c, file = "./plotly_objs/smooth_frame_n2c.rda")
save(date_vec_n1a, file = "./plotly_objs/date_vec_n1a.rda")
save(date_vec_n2a, file = "./plotly_objs/date_vec_n2a.rda")
save(date_vec_n1b, file = "./plotly_objs/date_vec_n1b.rda")
save(date_vec_n2b, file = "./plotly_objs/date_vec_n2b.rda")
save(date_vec_n1c, file = "./plotly_objs/date_vec_n1c.rda")
save(date_vec_n2c, file = "./plotly_objs/date_vec_n2c.rda")
save(n1_ymina, file = "./plotly_objs/n1_ymina.rda")
save(n1_ymaxa, file = "./plotly_objs/n1_ymaxa.rda")
save(n2_ymina, file = "./plotly_objs/n2_ymina.rda")
save(n2_ymaxa, file = "./plotly_objs/n2_ymaxa.rda")

save(n1_yminb, file = "./plotly_objs/n1_yminb.rda")
save(n1_ymaxb, file = "./plotly_objs/n1_ymaxb.rda")
save(n2_yminb, file = "./plotly_objs/n2_yminb.rda")
save(n2_ymaxb, file = "./plotly_objs/n2_ymaxb.rda")

save(n1_yminc, file = "./plotly_objs/n1_yminc.rda")
save(n1_ymaxc, file = "./plotly_objs/n1_ymaxc.rda")
save(n2_yminc, file = "./plotly_objs/n2_yminc.rda")
save(n2_ymaxc, file = "./plotly_objs/n2_ymaxc.rda")